package com.zjt.myjiakao.gdt;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Toast;


import com.qq.e.ads.cfg.VideoOption;
import com.qq.e.ads.nativ.NativeExpressAD;

import static android.content.ContentValues.TAG;


public class UtilAd {
	
	
	//显示屏幕外chaping
	public static void showBackAd(Activity activity){
		
		//activity.finish();
		UtilDialog.showExitAppDialog(activity);
		
	}			
			
	
	
	//
	public static void showInScreenAd(Activity activity){	
		
		//APIS.request(activity,2,false);

	}		
	

	// 显示小米Banner
	public static void showXiaoMiBanner(ViewGroup container) {

	}

    public static void showCustomAd(final ViewGroup container,Context context) {
		try {
			/**
			 *  如果选择支持视频的模版样式，请使用{@link Constants#NativeExpressSupportVideoPosID}
			 */
			NativeExpressAD nativeExpressAD = new NativeExpressAD((Activity)context, new com.qq.e.ads.nativ.ADSize(com.qq.e.ads.nativ.ADSize.FULL_WIDTH, com.qq.e.ads.nativ.ADSize.AUTO_HEIGHT), PositionId.APPID, PositionId.UNIFIED_VIDEO_PICTURE_ID_LARGE, new MyNativeExpressADListener(container)); // 这里的Context必须为Activity
			nativeExpressAD.setVideoOption(new VideoOption.Builder()
					.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // 设置什么网络环境下可以自动播放视频
					.setAutoPlayMuted(true) // 设置自动播放视频时，是否静音
					.build()); // setVideoOption是可选的，开发者可根据需要选择是否配置
			nativeExpressAD.setVideoPlayPolicy(getVideoPlayPolicy(VideoOption.AutoPlayPolicy.ALWAYS, context));  // 本次拉回的视频广告，在用户看来是否为自动播放的
			nativeExpressAD.loadAD(1);
		} catch (NumberFormatException e) {
			Log.w(TAG, "ad size invalid.");
			Toast.makeText((Activity)context, "请输入合法的宽高数值", Toast.LENGTH_SHORT).show();
		}
	}

	public static int getVideoPlayPolicy(int autoPlayPolicy, Context context){
		if(autoPlayPolicy == VideoOption.AutoPlayPolicy.ALWAYS){
			return VideoOption.VideoPlayPolicy.AUTO;
		}else if(autoPlayPolicy == VideoOption.AutoPlayPolicy.WIFI){
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
			return wifiNetworkInfo != null && wifiNetworkInfo.isConnected() ? VideoOption.VideoPlayPolicy.AUTO
					: VideoOption.VideoPlayPolicy.MANUAL;
		}else if(autoPlayPolicy == VideoOption.AutoPlayPolicy.NEVER){
			return VideoOption.VideoPlayPolicy.MANUAL;
		}
		return VideoOption.VideoPlayPolicy.UNKNOWN;
	}

//	public static void showSmallCustomAd(final ViewGroup container,Context context) {
//		try {
//
//			/**
//			 *  如果选择支持视频的模版样式，请使用{@link Constants#NativeExpressSupportVideoPosID}
//			 */
//			NativeExpressAD nativeExpressAD = new NativeExpressAD((Activity)context, new com.qq.e.ads.nativ.ADSize(com.qq.e.ads.nativ.ADSize.FULL_WIDTH, com.qq.e.ads.nativ.ADSize.AUTO_HEIGHT), Constants.APPID, Constants.native_small, new MyNativeExpressADListener(container)); // 这里的Context必须为Activity
//			nativeExpressAD.setVideoOption(new VideoOption.Builder()
//					.setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI) // 设置什么网络环境下可以自动播放视频
//					.setAutoPlayMuted(true) // 设置自动播放视频时，是否静音
//					.build()); // setVideoOption是可选的，开发者可根据需要选择是否配置
//			nativeExpressAD.loadAD(1);
//		} catch (NumberFormatException e) {
//			Log.w(TAG, "ad size invalid.");
//			Toast.makeText((Activity)context, "请输入合法的宽高数值", Toast.LENGTH_SHORT).show();
//		}
//
//	}

}
